import request from '../utils/request'

interface WalletApi {
  getBalance: (sync?: boolean) => Promise<any>
  getChainBalance: (currency: string, chain: string) => Promise<any>
  getAddress: (currency: string, chain: string) => Promise<any>
  generateDepositAddress: (currency: string, chain: string) => Promise<any>
  transfer: (data: { to: string; amount: number; currency: string; memo?: string }) => Promise<any>
  withdraw: (data: { to: string; amount: number; currency: string; memo?: string }) => Promise<any>
  getTransactions: (params?: { page?: number; pageSize?: number; type?: 'deposit' | 'withdraw' | 'transfer'; currency?: string }) => Promise<any>
  getTransactionDetail: (txId: string) => Promise<any>
  checkDeposit: (currency: string, chain: string) => Promise<any>
  getDebugTransactions: (params?: { currency?: string; type?: 'deposit' | 'withdraw' | 'transfer' }) => Promise<any>
}

export const walletApi: WalletApi = {
  // 获取钱包余额
  getBalance: (sync: boolean = false) => {
    return request.get('/wallet/balance', { params: { sync: sync ? 'true' : 'false' } })
  },
  
  // 查询链上余额（实时查询区块链）
  getChainBalance: (currency: string, chain: string) => {
    return request.get('/wallet/balance/chain', { params: { currency, chain } })
  },
  
  // 获取钱包地址
  getAddress: (currency: string, chain: string) => {
    return request.get(`/wallet/address/${currency}`, { params: { chain } })
  },
  
  // 生成充值地址
  generateDepositAddress: (currency: string, chain: string) => {
    return request.post('/wallet/deposit-address', { currency, chain })
  },
  
  // 转账
  transfer: (data: {
    to: string
    amount: number
    currency: string
    memo?: string
  }) => {
    return request.post('/wallet/transfer', data)
  },
  
  // 提现
  withdraw: (data: {
    to: string
    amount: number
    currency: string
    memo?: string
  }) => {
    return request.post('/wallet/withdraw', data)
  },
  
  // 获取交易记录
  getTransactions: (params?: {
    page?: number
    pageSize?: number
    type?: 'deposit' | 'withdraw' | 'transfer'
    currency?: string
  }) => {
    return request.get('/wallet/transactions', { params })
  },
  
  // 获取交易详情
  getTransactionDetail: (txId: string) => {
    return request.get(`/wallet/transactions/${txId}`)
  },
  
  // 手动检查充值
  checkDeposit: (currency: string, chain: string) => {
    return request.post('/wallet/check-deposit', { currency, chain })
  },
  
  // 调试接口：查看所有交易记录
  getDebugTransactions: (params?: {
    currency?: string
    type?: 'deposit' | 'withdraw' | 'transfer'
  }) => {
    return request.get('/wallet/debug/transactions', { params })
  }
}

